/**
 * Applikationsutveckling i Java, 5DV135
 * HT12
 * Project
 * 
 * File: MapManagerTest.java
 * Description: class that tests nullInvader.Engine.MapManager methods and variables.
 * Date: 13 december 2012
 * 
 * cs-user: dv11rlm
 * @author Robert Lundström
 */
package nullInvader.Engine;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;

import nullInvader.Engine.MapManager;
import nullInvader.Engine.Tower;
import nullInvader.Engine.Zone;

import org.junit.BeforeClass;
import org.junit.Test;

public class MapManagerTest {

    private static MapManager m;

    @BeforeClass
    static public void setUp() throws Exception {
	m = new MapManager();
    }

    @Test
    public void mapManagerCustomXML() {
	try {
	    m = new MapManager("/nullInvader/levels2.xml");
	} catch (Exception e) {
	    fail();
	}
    }

    @Test
    public void mapManagerNotNullTest() {
	assertNotNull(m);
    }

    @Test
    public void mapManagerXMLParseNotNull() {
	try {
	    assertNotNull(m
		    .parseXmlDom("/nullInvader/Resources/levels.xml"));
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    @Test
    public void mapManagerXMLSchemaNotNull() throws Exception {
	assertNotNull(m
		.loadSchema("/nullInvader/Resources/levelsXMLschema.xsd"));
    }

    @Test
    public void xmlValidationExceptionSuccessful() throws Exception {
	try {
	    m.validateXml(
		    m.loadSchema("/nullInvader/Resources/levelsXMLschema.xsd"),
		    m.parseXmlDom("/nullInvader/Resources/levels.xml"));
	} catch (IOException e) {
	    fail();
	}
    }

    @Test
    public void xmlValidationExceptionFailLoad() throws Exception {
	try {
	    m.validateXml(m.loadSchema("Should cause exception"),
		    m.parseXmlDom("nullInvader/Resources/levels.xml"));
	    fail();
	} catch (Exception e) {

	}
    }

    public void xmlValidationExceptionFailParse() throws Exception {
	try {
	    m.validateXml(
		    m.loadSchema("nullInvader/Resources/levelsXMLschema.xsd"),
		    m.parseXmlDom("Should cause exception"));
	    fail();
	} catch (Exception e) {

	}
    }

    public void mapTowersNotnull() {
	ArrayList<Tower> towerList = m.getMap().getTowerList();
	for (Tower t : towerList) {
	    assertNotNull(t);
	}
    }

    @Test
    public void mapTowersNotMoveable() {
	ArrayList<Tower> t = m.getMap().getTowerList();
	for (Tower t1 : t) {
	    assertEquals(false, t1.isMovable());
	}
    }

    @Test
    public void mapTowersReachablesNotNull() {
	ArrayList<Tower> t = m.getMap().getTowerList();
	for (Tower t1 : t) {
	    ArrayList<Zone> reachables = t1.getReachableZones();
	    for (Zone z : reachables) {
		assertNotNull(z);
	    }
	}
    }

    @Test
    public void mapManagerGetZones() {
	assertNotNull(m.getMap().getZone(new Point(0, 0)));
    }

    @Test
    public void mapManagerResetTest() throws Exception {
	m.resetCurrentMap();
	assertNotNull(m.getMap());
	assertNotNull(m.getMap().getZone(new Point(0, 0)));
    }

    @Test
    public void mapManagerCreateBrickClassTest() throws Exception {
	Field b = m.createBrickClass("Road", "true");
	assertNotNull(b);
    }

    @Test
    public void mapManagerCreateBrickClassIsMovableTest()
	    throws Exception {
	Field b = m.createBrickClass("Road", "true");
	assertEquals(true, b.isMovable());
    }

    @Test
    public void mapManagerCreateBrickClassIsNotMovableTest()
	    throws Exception {
	Field b = m.createBrickClass("Road", "false");
	assertEquals(false, b.isMovable());
    }

    @Test
    public void mapManagerCreateBrickGetIconClassTest()
	    throws Exception {
	Field b = m.createBrickClass("Road", "true");
	assertNotNull(b.getIcon());
    }

    @Test
    public void mapManagerCreateBrickClassException() {
	try {
	    @SuppressWarnings("unused")
	    Field b = m.createBrickClass("ShouldCauseException",
		    "true?");
	    /*
	     * if method don't throw exception either a class
	     * ShouldCauseException exist or the method failed
	     */
	    fail();
	} catch (Exception e) {
	    // method should cause exception so it was successful.
	}
    }

    @Test
    public void mapManagerGetNextMap() {
	Map m2 = m.getNextMap();
	assertNotNull(m2);
    }

    @Test
    public void mapManagercounterUpdate() throws Exception {
	m = new MapManager();
	int i = m.mapCounter;
	m.getNextMap();
	assertEquals(i + 1, m.mapCounter);
    }
}
